﻿Imports _1065081_1065104.KHDTO
Imports _1065081_1065104.KHBUS
Imports _1065081_1065104.Global_Class
Imports Excel = Microsoft.Office.Interop.Excel

Public Class frmnhanvien
    Dim _infornv As New Cinforkhachhang()
    Dim nvbus As New Cbusikhachhang()
    Dim _selectedRow As Integer
#Region "form moving"
    Private Declare Function ReleaseCapture Lib "user32" () As Long
    Private Declare Function sengmassage Lib "user32" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    Private Sub BorderlessWindowDrag(ByVal sender As Object, ByVal e As MouseEventArgs)
        ReleaseCapture()
        sengmassage(Me.Handle, &HA1, 2, 0&)
    End Sub
    Private Sub frmnhanvien_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        BorderlessWindowDrag(sender, e)
    End Sub
  
#End Region

#Region "Windows Event"
    Private Sub frmnhanvien_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        sua.Enabled = False
        xoa.Enabled = False

    End Sub

#End Region


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        txtid.Text = ""
        txtname.Text = ""
        tel.Text = ""
        identity.Text = ""
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try
            DataGridView1.DataSource = nvbus.Search(txtid.Text.Trim(), txtname.Text.Trim(), identity.Text.Trim(), tel.Text.Trim())
        Catch ex As Exception
            Throw ex

        End Try
    End Sub

    Private Sub them_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles them.Click
        CGlobalVariable.frmaddnhanvien = New frmaddnhanvien()
        CGlobalVariable.frmaddnhanvien.Show()

    End Sub

    Private Sub xoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xoa.Click
        Try
            nvbus.Delete(txtid.Text.Trim())
            DataGridView1.Rows.RemoveAt(_selectedRow)
            xoa.Enabled = False
            renew()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub sua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sua.Click
       


    End Sub
#Region "process"
    Private Function CheckDataInput() As Boolean
        Dim strErr As String = String.Empty

        If txtid.Text.Trim() = "" Then
            strErr = "- Mã khách hàng."
        End If
        If txtname.Text.Trim() = "" Then
            strErr = strErr & vbLf & "- Tên khách hàng."
        End If
        If identity.Text.Trim() = "" Then
            strErr = strErr & vbLf & "- Số chứng minh nhân dân."
        End If
        If adre2.Text.Trim() = "" Then
            strErr = strErr & vbLf & "- Địa chỉ khách hàng."
        End If
        If DateTime.Now.Year - DateTimePicker1.Value.Year < 15 Then
            strErr = strErr & vbLf & "- Ngày sinh không hợp lệ."
        End If

        If strErr <> String.Empty Then
            MessageBox.Show("Bạn phải hoàn tất các thông tin sau :" & vbLf & strErr, "Canh bao !", MessageBoxButtons.OK, MessageBoxIcon.[Error])
            Return False
        End If

        Return True
    End Function
    Private Function AddCustomerValue() As Cinforkhachhang
        Dim cus As New Cinforkhachhang()

        cus.nhanvienid = txtid.Text.Trim()
        cus.nhanvienname = txtname.Text.Trim()
        cus.indentitycard = identity.Text.Trim()
        cus.birthday = DateTimePicker1.Value
        ' cus.address = adre2.Text.Trim()
        cus.phonenumber = tel2.Text.Trim()
        cus.email = email2.Text.Trim()

        Return cus
    End Function
    Private Sub renew()
        txtid.Text = ""
        txtname.Text = ""
        identity.Text = ""
        adre2.Text = ""
        tel2.Text = ""
        email2.Text = ""

        xoa.Enabled = False
        sua.Enabled = False
    End Sub
    Private Sub AddDataToList(ByVal cus As Cinforkhachhang)
        DataGridView1.Rows(_selectedRow).Cells(0).Value = cus.nhanvienid
        DataGridView1.Rows(_selectedRow).Cells(1).Value = cus.nhanvienname
        DataGridView1.Rows(_selectedRow).Cells(2).Value = cus.indentitycard
        DataGridView1.Rows(_selectedRow).Cells(3).Value = cus.birthday
        'DataGridView1.Rows(_selectedRow).Cells(4).Value = cus.a
        DataGridView1.Rows(_selectedRow).Cells(5).Value = cus.PhoneNumber
        DataGridView1.Rows(_selectedRow).Cells(6).Value = cus.EMail
    End Sub
#End Region

   
    Private Sub xuatfile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xuatfile.Click

        Dim xlApp As Excel.Application
        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet
        Dim misValue As Object = System.Reflection.Missing.Value
        Dim i As Integer
        Dim j As Integer

        xlApp = New Excel.Application
        xlWorkBook = xlApp.Workbooks.Add(misValue)
        xlWorkSheet = xlWorkBook.Sheets("sheet1")

        For i = 0 To DataGridView1.RowCount - 2
            For j = 0 To DataGridView1.ColumnCount - 1
                xlWorkSheet.Cells(i + 1, j + 1) = _
                    DataGridView1(j, i).Value.ToString()
            Next
        Next

        xlWorkSheet.SaveAs("D:\xuatexcel.xlsx")
        xlWorkBook.Close()
        xlApp.Quit()

        releaseObject(xlApp)
        releaseObject(xlWorkBook)
        releaseObject(xlWorkSheet)

        MsgBox(" Bạn có thể tìm file tại địa chỉ D:\xuatexcel.xlsx")
    End Sub
    Private Sub releaseObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try
    End Sub

    Private Sub loaddulieu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles loaddulieu.Click
        Try
            DataGridView1.DataSource = nvbus.Search(txtid.Text.Trim(), txtname.Text.Trim(), identity.Text.Trim(), tel.Text.Trim())
        Catch ex As Exception
            Throw ex

        End Try
    End Sub

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

    End Sub

    Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter

    End Sub

    Private Sub GroupBox2_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox2.Enter

    End Sub
End Class